package com.blb.thread;

public class WaitDemo {

//    private Object obj = new Object();

    public synchronized void testWait(){
        //obj作为锁对象
//        synchronized (obj){
            System.out.println( Thread.currentThread().getName() + "执行testWait，等待");
            try {
                //锁可以调用wait
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "结束testWait");
//        }
    }

    public synchronized void testNotify(){
//        synchronized (obj) {
            //通知线程
            this.notifyAll();
//        }
    }

    public static void main(String[] args){
        WaitDemo demo = new WaitDemo();
        //子线程等待
        for (int i = 0; i < 5; i++) {
            new Thread(() -> {
                demo.testWait();
            }).start();
        }
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //主线程通知子线程
        demo.testNotify();
    }
}
